package environment;
import java.awt.Toolkit;
import java.util.ArrayList;


import boids.predators.*;
import boids.preys.*;

import util.Vector;


public class World
{
	private ArrayList<Prey> boids;
	private ArrayList<Predator> predators;
	
	private int width, height;
	
	public World(int someWidth, int someHeight)
	{		
		width = someWidth;
		height = someHeight;
		
		boids = new ArrayList<Prey>();
		
//		boids.add(new LinearPrey(new Vector(100, 200), new Vector()));
//		boids.add(new LinearPrey(new Vector(110, 200), new Vector()));
//		boids.add(new LinearPrey(new Vector(120, 200), new Vector()));
//		boids.add(new LinearPrey(new Vector(100, 210), new Vector()));
//		boids.add(new LinearPrey(new Vector(110, 210), new Vector()));
//		boids.add(new LinearPrey(new Vector(120, 210), new Vector()));
		
//		for (int i =  0; i <= 800; i++)
//		{
//			boids.add(new ExponentialPrey(new Vector(Math.random() * width, Math.random() * height),
//					new Vector(Math.random() - Math.random(), Math.random() - Math.random()),
//					width,
//					height));
//		}
		
//		for (int i = width/3; i <= width - width/3; i += 20)
//		{
//			for (int j = height/3; j <= height - height/3; j += 20)
//			{
//				boids.add(new ExponentialPrey(new Vector(i, j), new Vector(), width, height));
//			}
//		}
		
		for (int i = 0; i <= 300; i++)
		{
			double xRand = width/4 + Math.random() * width/4;
			double yRand = height/4 + Math.random() * height/4;
			
			boids.add(new ExponentialPrey(new Vector(xRand, yRand), new Vector(), width, height));
		}
		
		predators = new ArrayList<Predator>();
	}
	
	public void update()
	{
		int counter = 0;

		for (Prey b : boids)
		{
			b.update(boids, predators);
		}
				
		double averageVeloctiy = 0;
		
		for (Prey b: boids)
		{
			b.cleanUpdate();
			averageVeloctiy += b.getVelocity().magnitude();
		}
		
		System.out.println(averageVeloctiy / boids.size());
		
//		for (Predator p : predators)
//		{
//			p.update(boids);
//		}
	}
	
	private void simpleWrapAround(Prey b)
	{
		Vector location = b.getLocation();
		if (location.getX() < 0) b.setLocation(new Vector(width + location.getX(), location.getY()));
		if (location.getX() > width) b.setLocation(new Vector(location.getX() - width, location.getY()));
		if (location.getY() < 0) b.setLocation(new Vector(location.getX(), height + location.getY()));
		if (location.getY() > height) b.setLocation(new Vector(location.getX(), location.getY() - height));
	}
	
	public void updatePredator(Vector location)
	{
		predators = new ArrayList<Predator>();
		predators.add(new TestPredator(location));
	}
	
	public void manualPredatorUpdate(Vector direction)
	{
		for (Predator p : predators)
		{
			p.manualUpdate(direction);
		}
	}
	
	public ArrayList<Prey> getBoids()
	{
		return boids;
	}
	
	public ArrayList<Predator> getPredators()
	{
		return predators;
	}
}
